/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.dto.sqlServer.PlcLayUp;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.PlcLayUpRepository;
import me.zhengjie.service.PlcLayUpService;
import me.zhengjie.service.dto.PlcLayUpDto;
import me.zhengjie.service.dto.PlcLayUpQueryCriteria;
import me.zhengjie.service.mapper.PlcLayUpMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author zhangYY
* @date 2024-01-13
**/
@Service
@RequiredArgsConstructor
public class PlcLayUpServiceImpl implements PlcLayUpService {

    private final PlcLayUpRepository plcLayUpRepository;
    private final PlcLayUpMapper plcLayUpMapper;

    @Override
    public PageResult<PlcLayUpDto> queryAll(PlcLayUpQueryCriteria criteria, Pageable pageable){
        Page<PlcLayUp> page = plcLayUpRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(plcLayUpMapper::toDto));
    }

    @Override
    public List<PlcLayUpDto> queryAll(PlcLayUpQueryCriteria criteria){
        return plcLayUpMapper.toDto(plcLayUpRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PlcLayUpDto findById(Integer id) {
        PlcLayUp plcLayUp = plcLayUpRepository.findById(id).orElseGet(PlcLayUp::new);
        ValidationUtil.isNull(plcLayUp.getId(),"PlcLayUp","id",id);
        return plcLayUpMapper.toDto(plcLayUp);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PlcLayUp resources) {
        plcLayUpRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PlcLayUp resources) {
        PlcLayUp plcLayUp = plcLayUpRepository.findById(resources.getId()).orElseGet(PlcLayUp::new);
        ValidationUtil.isNull( plcLayUp.getId(),"PlcLayUp","id",resources.getId());
        plcLayUp.copy(resources);
        plcLayUpRepository.save(plcLayUp);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            plcLayUpRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PlcLayUpDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PlcLayUpDto plcLayUp : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("ip", plcLayUp.getIp());
            map.put("内容", plcLayUp.getContent());
            map.put("创建时间", plcLayUp.getCreateTime());
            map.put("更新时间", plcLayUp.getUpdateTime());
            map.put("plc状态", plcLayUp.getPlcStatus());
            map.put("有无载", plcLayUp.getHasPallet());
            map.put("port", plcLayUp.getPort());
            map.put("启动按钮", plcLayUp.getButtonSignal());
            map.put("回流线启动信号", plcLayUp.getStartSignal());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}